<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>
            3D化学后台管理系统
        </title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="format-detection" content="telephone=no">
        <link rel="stylesheet" href="./css/x-admin.css" media="all">
    </head>
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>用户管理</cite></a>
              <a><cite>用户列表</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
        </div>
        <div class="x-body">
            <form id="form-query" class="layui-form x-center" action="" >
                <div class="layui-form-pane" style="margin-top: 15px;">
                  <div class="layui-form-item">
                    <label class="layui-form-label">查询</label>
                    <div class="layui-input-inline">
                        <input type="text" id="input-userName" name="userName"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
                    </div>

                    <div class="layui-input-inline">
                        <input type="text" id="input-nickName" name="nickName"  placeholder="请输入用户昵称" autocomplete="off" class="layui-input">
                    </div>

                    <div class="layui-input-inline">
                        <input type="text" id="input-gender" name="gender"  placeholder="请输入性别" autocomplete="off" class="layui-input">
                    </div>
                   
                    <div class="layui-input-inline" style="width:80px">
                        <button id="btn-query" class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
                    </div>
                  </div>
                </div> 
            </form>
            <xblock>
                <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon">&#xe640;</i>批量删除</button>
                <button class="layui-btn" onclick="user_add('添加用户','user-add.html','','','600','500')"><i class="layui-icon">&#xe608;</i>添加</button>
                <span id="span-count" class="x-right" style="line-height:40px">共有数据：0 条</span></xblock>
            <table class="layui-table">
                <thead>
                    <tr>
                        <th>
                            <input type="checkbox" name="" value="">
                        </th>
                        <th>
                            ID
                        </th>
                        <th>
                            用户名
                        </th>
                        <th>
                            头像
                        </th>
                        <th>
                            性别
                        </th>
                        <th>
                            昵称
                        </th>
                        <th>
                            操作
                        </th>
                    </tr>
                </thead>
                <tbody id="user-list">
                    <tr style="display: none;">
                        <td id="td-check">
                            <input type="checkbox" value="1" name="">
                        </td>
                        <td id="td-userId">
                            1
                        </td>
                        <td id="td-icon">
                            <img  src="" width="80" alt="">
                        </td>
                        <td id="td-userName">
                            小梦
                        </td>
                        <td id="td-gender">
                            男
                        </td>
                        <td id="td-nickName">
                            小明明
                        </td>
                        
                        <td class="td-manage" id="td-manage">
                            
                            <a title="编辑" href="javascript:;" onclick="user_edit(event,'编辑','user-edit.html','','','600','500')"
                            class="ml-5" style="text-decoration:none">
                                <i class="layui-icon">&#xe642;</i>
                            </a>
                            <a style="text-decoration:none"  onclick="user_password(event,'修改密码','user-password.html','','','600','500')"
                            href="javascript:;" title="修改密码">
                                <i class="layui-icon">&#xe631;</i>
                            </a>
                            <a title="删除" href="javascript:;" onclick="user_del(event)" 
                            style="text-decoration:none">
                                <i class="layui-icon">&#xe640;</i>
                            </a>
                        </td>
                    </tr>
                </tbody>
            </table>

            <div id="page" class="layui-laypage"></div>

        </div>
        <script src='./js/jquery.min.js' type='text/javascript' ></script>
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script src="./js/x-layui.js" charset="utf-8"></script>
        <script src="http-config.js"></script>
        <script> 
            
            layui.use(['laydate','element','laypage','layer'], function(){
                $ = layui.jquery;//jquery
              laydate = layui.laydate;//日期插件
              lement = layui.element();//面包导航
              laypage = layui.laypage;//分页
              layer = layui.layer;//弹出层

              //以上模块根据需要引入
              layer.ready(function(){ //为了layer.ext.js加载完毕再执行
                  layer.photos({
                    photos: '#user-list'
                    ,shift: 0 //0-6的选择，指定弹出图片动画类型，默认随机
                  });
                }); 

              

              //分页器
                $.ajax({// 发送网络请求获取数据
                    url: baseURL + "/student_info/list",
                    type: 'GET',
                    success: function (data) {
                        var totalPages = data.totalPages;
                        var totalElements = data.totalElements;
                        var pageSize = data.size;
                        var currentPage = data.number + 1;
                        
                        document.getElementById("span-count").innerHTML = '共有数据：' + totalElements + ' 条'

                        var pageConfig = {
                        cont: 'page', // 分页容器的 ID
                        pages: totalPages, // 总页数
                        limit: pageSize, // 每页显示的记录数
                        curr: currentPage, // 当前页码
                        jump: function (obj, first) {
                            if (!first) {
                                currentPage = obj.curr;
                                $.ajax({
                                    url:baseURL + "/student_info/list?pageNo=" + currentPage,
                                    contentType:"application/json; charset=utf-8",
                                    success:function (data){
                                    showlist(data.content);
                                    }
                                })
                            }
                        }
                        };
                        
                        // 渲染分页器
                        laypage(pageConfig);
                        showlist(data.content);
                    },
                    error: function () {
                        layer.msg('网络请求发生错误');
                    }
                });

                /*用户-模糊查询*/
              $('#form-query').submit(function(event){
                        // 阻止表单默认的提交行为
                        event.preventDefault();
                        const userName = $("#input-userName").val();
                        const nickName = $("#input-nickName").val();
                        const gender = $("#input-gender").val();

                        var url = baseURL + "/student_info/findStudentInfosByUserNameLikeAndNickNameLikeAndGenderIs?"; // 请求URL
                        if (userName !== null) url += "userName=" + userName + "&";
                        if (nickName !== null) url += "nickName=" + nickName + "&";
                        if (gender !== null) url += "gender=" + gender + "&";

                        // 去掉URL最后一个&符号
                        if (url.endsWith("&")) {
                            url = url.slice(0, -1);
                        }
                        console.log(url)

                        $.ajax({
                            url: url,
                            type: 'GET',
                            contentType:"application/json; charset=utf-8",
                            success:function(data){
                                var totalPages = data.totalPages;
                                var totalElements = data.totalElements;
                                var pageSize = data.size;
                                var currentPage = data.number + 1;
                                
                                document.getElementById("span-count").innerHTML = '共有数据：' + totalElements + ' 条'

                                var pageConfig = {
                                    cont: 'page', // 分页容器的 ID
                                    pages: totalPages, // 总页数
                                    limit: pageSize, // 每页显示的记录数
                                    curr: currentPage, // 当前页码
                                    jump: function (obj, first) {
                                        if (!first) {
                                            currentPage = obj.curr;
                                            $.ajax({
                                                url:url + "&pageNo=" + currentPage,
                                                contentType:"application/json; charset=utf-8",
                                                success:function (data){
                                                showlist(data.content);
                                                }
                                            })
                                        }
                                    }
                                };
                                
                                // 渲染分页器
                                laypage(pageConfig);
                                showlist(data.content);
                            },
                        });
                    });

              
            });

            //批量删除提交
             function delAll () {
                layer.confirm('确认要删除吗？',function(index){
                    // 获取tbody
                    var tbody = document.getElementById("user-list");
                    var rows = tbody.getElementsByTagName("tr");
                    for(let i = 1; i < rows.length; i++){
                        var userId = rows[i].cells[1].textContent;  // 获取第二个单元格的文本内容
                        var checked = rows[i].cells[0].querySelector('input[type="checkbox"]').checked;  // 获取第二个单元格的文本内容
                        console.log("i:" + i+"-----"+checked);
                        if(checked){
                            $.ajax({
                                url:baseURL + "/student_info/deleteStudentInfoByUserId/" + userId,
                                contentType:"application/json; charset=utf-8",
                                success:function (data){
                                    console.log(data);
                                }
                            });
                            console.log(i);
                        }
                    }
                    //捉到所有被选中的
                    location.reload();//刷新父页面
                    layer.msg('删除成功', {icon: 1});
                });

             }
             /*用户-添加*/
            function user_add(title,url,id,name,w,h){
                x_admin_show(title,url,id,name,w,h);
            }

            /*用户-查看*/
            function user_show(title,url,id,w,h){
                x_admin_show(title,url,id,w,h);
            }

            // 用户-编辑
            function user_edit (event,title,url,id,name,w,h) {
                var button = event.target;  // 获取当前点击的按钮元素
                var row = button.parentElement.parentElement.parentElement;  // 获取该行的元素
                var userId = row.cells[1].textContent;  // 获取第二个单元格的文本内容
                var userName = row.cells[2].textContent;
                var nickName= row.cells[5].textContent;
                layer.open({
                    type: 2,
                    area: [w+'px', h +'px'],
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade:0.4,
                    title: title,
                    content: url+
                    '?userId=' + userId + 
                    '&userName=' + userName + 
                    '&nickName=' + nickName
                });
            }
            /*密码-修改*/
            function user_password(event,title,url,id,name,w,h){
                var button = event.target;  // 获取当前点击的按钮元素
                var row = button.parentElement.parentElement.parentElement;  // 获取该行的元素
                var userId = row.cells[1].textContent;  // 获取第二个单元格的文本内容
                var userName = row.cells[2].textContent;
                x_admin_show(title,url,userId,userName,w,h);
            }
            /*用户-删除*/
            function user_del(event){
                var button = event.target;  // 获取当前点击的按钮元素
                var row = button.parentElement.parentElement.parentElement;  // 获取该行的元素
                var userId = row.cells[1].textContent;  // 获取第二个单元格的文本内容
                
                layer.confirm('确认要删除吗？',function(index){
                    $.ajax({
						url:baseURL + "/student_info/deleteStudentInfoByUserId/" + userId,
						contentType:"application/json; charset=utf-8",
						success:function (data){
                           console.log(data);
						}
					});
                    //发异步删除数据
                    row.remove();
                    location.reload();//刷新父页面
                    layer.msg('已删除!',{icon:1,time:1000});
                });
            }
            function showlist(data){
                //提前获取数据
                var checkinnerHTML = document.getElementById('td-check').innerHTML;
                var manageinnerHTML = document.getElementById('td-manage').innerHTML;

                // 获取tbody
                var tbody = document.getElementById("user-list");
                var rows = tbody.getElementsByTagName("tr");
                for(var i = 0; i < rows.length; i++){
                    rows[i].style.display = "none";
                }

                for(var i = 0; i < data.length; i++){
                    // 创建一个行元素
                    var row = document.createElement('tr');
                    // 创建td单元格 
                    var check = document.createElement('td'); check.id = "td-check";
                    var userId = document.createElement('td');
                    var userName = document.createElement('td');
                    var icon = document.createElement('td');
                    var gender = document.createElement('td');
                    var nickName = document.createElement('td');
                    var manage = document.createElement('td'); manage.id = "td-manage";

                    //显示数据
                    check.innerHTML = checkinnerHTML;
                    userId.innerHTML = data[i].userId;
                    userName.innerHTML = data[i].userName;
                    icon.innerHTML = '<img  src="'+ 'data:image/png;base64,' + data[i].icon + '" width="80" alt="">';
                    gender.innerHTML = data[i].gender;
                    nickName.innerHTML = data[i].nickName;
                    manage.innerHTML = manageinnerHTML;            

                    // 将新建的td加入新建的tr行
                    row.appendChild(check);
                    row.appendChild(userId);
                    row.appendChild(userName);
                    row.appendChild(icon);
                    row.appendChild(gender);
                    row.appendChild(nickName);
                    row.appendChild(manage);
                        
                    // 将这行加到tbody
                    tbody.appendChild(row);
                }
            }  
            </script>
            <script>
                var _hmt = _hmt || [];
                (function() {
                var hm = document.createElement("script");
                hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
                var s = document.getElementsByTagName("script")[0]; 
                s.parentNode.insertBefore(hm, s);
                })();
        </script>
    </body>
</html>